package org.excample.service.impl;

import org.excample.entity.User;
import org.excample.mapper.UserMapper;
import org.excample.service.UserService;
import org.excample.utils.Md5Util;
import org.excample.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service//业务层
public class UserServiceImpl implements UserService {
    @Autowired//注入，会将代理生成的实现类注入到该引用中
    private UserMapper userMapper;
    @Override
    public User findUserByName(String username) {
        //调用MyBatis代理生成实现类型功能方法
        return userMapper.selectUserByName(username);
    }

    @Override
    public void register(String username, String password) {
        //密码加密
        String md5String = Md5Util.getMD5String(password);
        userMapper.insertUser(username, md5String);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdate_time(new Date());
        userMapper.updateUser(user);
    }

    @Override//该注解表示重写，重写接口里的方法
    public void updateAvatar(String avatarUrl) {
        //从线程池中获得当前登录者的id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
    }
}
